// pages/goods/index.js
var app = getApp();
var WxParse = require('../../wxParse/wxParse.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
   data:{},
   popup:0,
   choose_property_data:{},
   choose_spec:{},
    disabled:0,
    spec_keys_str:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({ extConfig: app.getExtConfigSync() })
    this.data.id = options.id ? options.id : 155;
    if (options.scene) {
      this.data.id = decodeURIComponent(options.scene);
    }
    this.loadGoodInfo(); 
  },
  chooseSpec:function(){
    this.setData({ popup: 1 })
  },
  btn_cancel:function(){
    this.setData({ popup:0});
  },
  //选规格
  chooseProperty:function(e){
    //console.log(e); 
    var property = e.currentTarget.dataset.property;
    var subProperty = e.currentTarget.dataset.subProperty;
    var choose_property_data = this.data.choose_property_data;
    choose_property_data[property] = subProperty;
    this.setData({ choose_property_data: choose_property_data});
    var arr=[]
    for (var i in choose_property_data){
      arr.push(choose_property_data[i]);
    }

    if (arr.length == this.data.data.property_arr.length){
      this.setData({ spec_keys_str: arr.join(" ") });
      var l = arr.length;
      for (var j = 0; j < (5-l);j++ ){
        arr.push("");
      }
      // console.log(arr,arr);
      var spec_keys = arr.sort();
      var spec_keys_str = spec_keys.join("_");
      if (this.data.data.goods_spec[spec_keys_str] && this.data.data.goods_spec[spec_keys_str].spec_stock>0) {
        this.setData({ choose_spec: this.data.data.goods_spec[spec_keys_str] });
        this.setData({ disabled: 0 });
      }else{
        wx.showToast({
          title:'已售空',
          image:'/image/info.png'
        });
        this.setData({ disabled:1});
      }
    }
    
    // console.log(this.data.choose_spec);
    //choose_spec
  },
  //加载购物车
  loadGoodInfo(reload = 0) {
    var that = this;
    if (reload == 1) {
      this.setData({
        loading: 0,
        data: {},
      });
    }
    this.data.loading = 1;
    app.api("mall/goods/find", {
      id: this.data.id
    }, function (res) {
      that.data.loading = 0;
      if (res.code != 200) {
        app.alert(res.msg);
        return;
      }
      that.setData({
        data: res.data
      });
      WxParse.wxParse('article', 'html', res.data.content, that, 5);
      if (that.data.data.property_arr.length>0){
        that.setData({
          disabled: 1
      });
      }
    })
  },

  buyNow: function (e){
    var postData = {};
    var type = e.currentTarget.dataset.type;
    if (this.data.data.property_arr.length > 0 && this.data.choose_spec.spec_stock>0 ){
      postData.goods_id = this.data.choose_spec.goods_id;
      postData.goods_spec_id = this.data.choose_spec.id;
      //可以去结算
    } else if (this.data.data.property_arr.length==0){
      postData.goods_id = this.data.data.id;
    }else{
      this.chooseSpec();
      return ;
    }
    //加入购车
    this.data.loading = 1;
    var that = this;
    this.setData({
      loading:1,
      disabled:1
    });
    app.api("mall/cart/add", postData, function (res) {
      that.data.loading = 0;
      that.setData({
        loading: 0,
        disabled: 0
      });
      if (res.code != 200) {
        app.alert(res.msg);
        return;
      }
      if (type =="buyNow"){
        wx.navigateTo({
          url: '/pages/checkout/index?shop_id=' + res.data.shop_id
        });
      }else{

        wx.showModal({
          title: '提示',
          cancelText:'返回',
          confirmText:'去结算',
          content: '成功加入购物车',
          success(res) {
            if (res.confirm) {
              //console.log('用户点击确定')
              wx.navigateTo({
                url: '/pages/checkout/index',
              })
            } else if (res.cancel) {
              wx.switchTab({
                url: '/pages/index/index',
              })
             
            }
          }
        })

        // wx.showToast({
        //   title: '成功加入购物车',
        // });
        that.btn_cancel();
      }
      
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.loadGoodInfo(1);
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
    //浏览图片
  previewListImage: function (e) {
    var current = e.target.dataset.src;
    var imglist = this.data.data.imglist;
    var pics =[];
    for (var i in imglist){
      pics.push(imglist[i].file_url);
    }
    wx.previewImage({
      current: current,
      urls: pics
    })
  }
 
})